﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Objects;
using Smart.Data.BaseClasses;
using Smart.Model;
using System.Data.Common;

namespace Smart.Data.Reporting
{
    /// <summary>
    /// Implement data logic for reporting
    /// </summary>
    public class ReportingDL : BaseDataAccess
    {
        private ReportingDL instance;

        public ReportingDL(ISmartEntities context)
        {
            Context = context;
        }

        public ReportingDL GetInstance(ISmartEntities context = null)
        {
            if (instance == null)
            {
                instance = new ReportingDL(context);
            }
            return instance;
        }

        public List<MealsReportView> GetMealsReport(int currentUserID)
        {
            // real DB Access
            List<MealsReportView> result = new List<MealsReportView>();
            foreach (var item in Context.MealsReportView.ToList<MealsReportView>())
            {
                if (item.UserId == currentUserID)
                {
                    result.Add(item);
                }
            }
            return result;
        }

        public List<ExerciseReportView> GetSportsReport(int currentUserID)
        {
            List<ExerciseReportView> result = new List<ExerciseReportView>();
            foreach (var item in Context.ExerciseReportView.ToList<ExerciseReportView>())
            {
                if (item.UserId == currentUserID)
                {
                    result.Add(item);
                }
            }
            return result;
        }

        public double getAllCaloriesForMeal(int currentUserID, string mealName)
        {
            var query = (from t in Context.MealsReportView where t.MealName == mealName && t.UserId == currentUserID
                         group t by new {t.MealName} into g
                            let SUM = g.Sum(uh => uh.Energy)
                            select new {SUM});
            double res = 0;
            if (query.FirstOrDefault() != null)
            {
                res = (double) query.FirstOrDefault().SUM;
            }
            return res;
        }

        public double getAllCaloriesForExercise(int currentUserID, string exerciseName)
        {
            var query = (from t in Context.ExerciseReportView where t.ExerciseName == exerciseName && t.UserId == currentUserID
                         group t by new {t.ExerciseName} into g
                            let SUM = g.Sum(uh => uh.BurnedCals)
                            select new {SUM});
            double res = 0;
            if (query.FirstOrDefault() != null)
            {
                res = (double) query.FirstOrDefault().SUM;
            }
            return res;
        }
        
        public List<Exercise> getAllExercises() {

            return Context.Exercise.ToList<Exercise>();
        }
    }
}
